var zcHttp = {
	KeyHttp: 'zcsoft_uni_insure_httpurl',
	KeySession: 'zcsoft_uni_insure_session',
	ServerUrl: '',
	WebSession: '',
	IsNeedLogin: 'true',
	getConfig: function (options) {
		let me = this;
		if(options.IsNeedLogin === false || options.IsNeedLogin === 'false')
			me.IsNeedLogin = 'false';
		else
			me.IsNeedLogin = 'true';

		// #ifdef H5
		me.ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
		// #endif
		
		// #ifdef MP-WEIXIN
		me.WebSession = uni.getStorageSync(me.KeySession);
		me.ServerUrl = uni.getStorageSync(me.KeyHttp) + '?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
		// #endif
		
	},
	doCheck: function (options) {
		let me = this;
		if (!me.ServerUrl) {
			// 要跳转到错误页,显示请配置服务器访问地址。
		}
	},
	Post: function (options) {
        try{
            let me = this;
            me.getConfig(options);
            me.doCheck(options);
            
            let PlateType = '';
            // #ifdef H5
            PlateType = 'H5';
            // #endif
            
            // #ifdef MP-WEIXIN
            PlateType = 'WxMP';
            // #endif
            let reqStr = '请求' + options.Paras.Name + '>' + options.Paras.Action;
            let resStr = '返回' + options.Paras.Name + '>' + options.Paras.Action;
            
            // 网络请求
            options.Paras.session = me.WebSession;
            uni.request({
              url: me.ServerUrl,
              data: options.Paras,
              method:"POST",
              dataType:"json",
              success: function (response) {
                  if (response.data.msg == '请重新登录!') {
                        uni.showModal({
                            title: '温馨提示',
                            content: ''+response.data.msg,
                            showCancel: false,
                            success: res => {
                                uni.removeStorageSync(me.KeySession)
                                wx.redirectTo({
                                    url:"../login/login?isLoading=false&isLogin=true"
                                })
                            }
                        });
                        return;
                  }
            	if(response.statusCode === 200) {
            		if(options.Callback) {
            			response.data.state = 'back';
            			options.Callback(response.data,options.Scope);
            		}
            		if(PlateType == 'WxMP'){
            			if(response && response.data && response.data.success) {
            				uni.setStorageSync(me.KeySession,response.data.session);
            			}
            		}
            	}
            	else {
            		uni.showModal({
            		    title: '警告',
            		    content: '网络超时，任务已取消!',
            			showCancel: false
            		});
            		response.data.state = 'err';
            		options.Callback(response.data,options.Scope);
            	}
              },
              fail: function (response){
            	  if(response.errMsg){
            		  if(response.errMsg.toLowerCase().trim() === 'request:fail') {
            			  uni.showModal({
            			      title: '警告',
            			      content: '已断网，请检查网络后重试!',
            				  showCancel: false
            			  });
            			  response.data.state = 'err';
            			  options.Callback(response.data,options.Scope);
            		  }
            	  }
              },
              complete:function(response) {
                if(!response){
                    response = {};
                }
                if(!response.data){
                    response.data = {};
                }
                if(options.Callback){
                    response.data.state = 'end';
                    options.Callback(response.data,options.Scope);
                }
              }
            });
        }catch(e){
            console.error('错误 = ',e);
        }
        
		
	},
	Log:function (scope) {
		let me = this;
		let desc = '';
		let page = '';
		let func = '';
		let msg = '';
		let data = '';
		if(scope.__route__)
			page = scope.__route__;
		if(scope._method_)
			func = scope._method_;
		if(scope._errmsg_)
			msg = scope._errmsg_;
		if(scope._data_){
			if(scope._data_.length >= 62000){
				data = scope._data_.substr(0,62000);
			}
			else{
				data = scope._data_;
			}
		}
			
		uni.showModal({
		    title: '错误',
		    content: msg,
			showCancel: false
		});

		// #ifdef MP-WEIXIN
		let IsCanSysInfo = uni.canIUse('getSystemInfoSync.return.screenWidth');
		if(IsCanSysInfo){
			let curSystem = uni.getSystemInfoSync();
			desc += '品牌:' + curSystem.brand;
			desc += ', 型号:' + curSystem.model;
			desc += ', 像素比:' + curSystem.pixelRatio;
			desc += ', 屏宽:' + curSystem.screenWidth;
			
			desc += ', 屏高:' + curSystem.screenHeight;
			desc += ', 系统版本:' + curSystem.system;
			desc += ', 微信版本:' + curSystem.version;
			desc += ', 平台:' + curSystem.platform;
		}
		// #endif

		me.Post({
			IsNeedLogin:'false',
			Paras: {
				Name: 'Uni_WxLog',
				Action: 'setWxLog',
				Data: {
					Page: page,
					Func: func,
					Msg: msg,
					Desc:desc,
					Data:data
				}
			}
		});
	},
	getDc: function () {
	  return new Date().getTime();
	},
    // 数据加密算法 封装方法
    Base64: function() {
            // private property  
            _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            // public method for encoding  
            this.encode = function (input) {
                let output = "";
                let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                let i = 0;
                input = _utf8_encode(input);
                while (i < input.length) {
                    chr1 = input.charCodeAt(i++);
                    chr2 = input.charCodeAt(i++);
                    chr3 = input.charCodeAt(i++);
                    enc1 = chr1 >> 2;
                    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                    enc4 = chr3 & 63;
                    if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                    } else if (isNaN(chr3)) {
                        enc4 = 64;
                    }
                    output = output +
                        _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                        _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
                }
                return output;
            }
    
            // public method for decoding  
            this.decode = function (input) {
                let output = "";
                let chr1, chr2, chr3;
                let enc1, enc2, enc3, enc4;
                let i = 0;
                input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
                while (i < input.length) {
                    enc1 = _keyStr.indexOf(input.charAt(i++));
                    enc2 = _keyStr.indexOf(input.charAt(i++));
                    enc3 = _keyStr.indexOf(input.charAt(i++));
                    enc4 = _keyStr.indexOf(input.charAt(i++));
                    chr1 = (enc1 << 2) | (enc2 >> 4);
                    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                    chr3 = ((enc3 & 3) << 6) | enc4;
                    output = output + String.fromCharCode(chr1);
                    if (enc3 != 64) {
                        output = output + String.fromCharCode(chr2);
                    }
                    if (enc4 != 64) {
                        output = output + String.fromCharCode(chr3);
                    }
                }
                output = _utf8_decode(output);
                return output;
            }
    
            // private method for UTF-8 encoding  
            _utf8_encode = function (string) {
                string = string.replace(/\r\n/g, "\n");
                let utftext = "";
                for (let n = 0; n < string.length; n++) {
                    let c = string.charCodeAt(n);
                    if (c < 128) {
                        utftext += String.fromCharCode(c);
                    } else if ((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
                    } else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
    
                }
                return utftext;
            }
    
            // private method for UTF-8 decoding  
            _utf8_decode = function (utftext) {
                let string = "";
                let i = 0;
                let c = c1 = c2 = 0;
                while (i < utftext.length) {
                    c = utftext.charCodeAt(i);
                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    } else if ((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i + 1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    } else {
                        c2 = utftext.charCodeAt(i + 1);
                        c3 = utftext.charCodeAt(i + 2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }
                }
                return string;
            }
        }
    
	
}

export default zcHttp;

/* 调用方式
import zcHttp from 'common/zcHttp';

zcHttp.Post({
	IsNeedLogin: false,
	Paras: {
		Name: '',
		Action: '',
		Data: {
			Id: 3,
			Code: '001'
		}
	},
	Callback:fnXXX,
	Scope: this
});
或
let me = this;
me.$ZcPublic.Server.Post({
	IsNeedLogin: false,
	Paras: {
		Name: '',
		Action: '',
		Data: {
			Id: 3,
			Code: '001'
		}
	},
	Callback:fnXXX,
	Scope: this
});
// 写日志
me.$ZcPublic.Server.Post()
*/